﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.En.Article
{
    public class ReadController : BaseTempController
    {
        public ReadController()
        {
            LayoutControllerType = typeof(EnController);
        }

        [NonVisit]
        public virtual void setpublic()
        {
            set("top", loadHtml(new En.PublicController().Top));
            set("bottom", loadHtml(new En.PublicController().Bottom));
        }

        [HttpGet]
        public virtual void Show(Int64 Id)
        {
            var read = db.RunTable<Document>("select ybb_document.*,ybb_document_content.*,ybb_document_attr.* from ybb_document left join ybb_document_content on ybb_document.id=ybb_document_content.did left join ybb_document_attr on ybb_document.id=ybb_document_attr.did where ybb_document.id=" + Id);
            var dtrow = read.Rows[0];
            if (dtrow["link"].ObjectToInt() == 1)
            {
                redirectDirect(dtrow["link"].ToString());
                return;
            }
            var t = TypeList.findById(dtrow["tid"].ObjectToInt());
            //模板
            var template = String.Empty;
            if (dtrow["istemplates"].ObjectToInt() == 1)
            {
                template = dtrow["templatename"].ToString();
            }
            else
            {
                template = t.readtemplate;
            }
            view(DefaultTemp + "Article/" + template);
            //标题、关键词、描述
            var title = String.Empty;
            var Keywords = String.Empty;
            var Description = String.Empty;
            var Position = String.Empty;
            var titlelist = new YBBCommon().GetParentStr(t.Id, true);
            var tl = TypeList.find(" Id in (" + titlelist + ")").list();
            if (dtrow["headtitle"].IsNull())
            {
                foreach (var row in tl)
                {
                    title = read.Rows[0]["title"].ToString();
                    title += "-" + row.typename;
                }
                title += "-" + YBBCommon.GetLangStr(CurrentLng, "sitename");
            }
            else
            {
                title += dtrow["headtitle"].ToString();
            }
            if (!dtrow["keywords"].IsNull())
            {
                Keywords = dtrow["keywords"].ToString();
            }
            else
            {
                Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
            }
            if (!dtrow["description"].IsNull())
            {
                Description = dtrow["description"].ToString();
            }
            else
            {
                Description = YBBCommon.GetLangStr(CurrentLng, "description");
            }
            ctx.Page.SetTitle(title);
            ctx.Page.Keywords = Keywords;
            ctx.Page.Description = Description;

            var dlist = new YBBCommon().GetParentStr(t.Id, true);
            tl = TypeList.find(" Id in (" + dlist + ")").list();
            var i = 0;
            foreach (var row in tl)
            {
                i++;
                if (i == 1)
                {
                    Position += "<span>" + row.typename + "</span>";
                }
                else
                {
                    Position = "<a href=\"" + UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, row.Id) + "\" >" + row.typename + "</a> &gt;&gt; " + Position;
                }
            }
            set("daohang", Position);

            m_prenext mp = new m_prenext();
            #region 上一篇
            var minid = db.RunScalar<Document>("select min(id) from ybb_document where id >" + Id + " and (tid = " + t.Id + " or extid like '%" + t.Id + "%') order by pid asc ,Id desc").ObjectToInt();
            if (minid > 0)
            {
                var mt = Document.findById(minid);
                if (mt != null)
                {
                    mp.Name = mt.title;
                    mp.Url = UrlLinkHelp.GetArticleReadLink(DefaultLng, CurrentLng, mt.Id);
                }
                else
                {
                    mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
                }
            }
            else
            {
                mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
            }
            bind("pre", mp);
            #endregion
            #region 下一篇
            mp = new m_prenext();
            var maxid = db.RunScalar<Document>("select min(id) from ybb_document where id <" + Id + " and (tid = " + t.Id + " or extid like '%" + t.Id + "%') order by pid asc ,Id desc").ObjectToInt();
            if (maxid > 0)
            {
                var mt = Document.findById(maxid);
                if (mt != null)
                {
                    mp.Name = mt.title;
                    mp.Url = UrlLinkHelp.GetArticleReadLink(DefaultLng, CurrentLng, mt.Id);
                }
                else
                {
                    mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
                }
            }
            else
            {
                mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
            }
            bind("next", mp);
            #endregion

            set("read", dtrow);
            set("document", Document.findById(Id));
            set("typeread", t);
            set("hits", Link.To(AddHits, Id));
            setpublic();
            SetPublic();
        }

        public virtual void AddHits(Int64 Id)
        {
            var d = Document.findById(Id);
            if (d != null)
            {
                d.click++;
                d.update();
                ctx.web.ResponseWrite("document.write('" + d.click + "')");
            }
            else
            {
                ctx.web.ResponseWrite("document.write('0')");
            }
            ctx.web.ResponseEnd();
        }
    }
}
